package com.wyc.serviceDriverUser.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyc.internalCommon.constant.CommonStatusEnum;
import com.wyc.internalCommon.constant.DriverUserConstant;
import com.wyc.internalCommon.result.ApiResult;
import com.wyc.serviceDriverUser.dto.DriverUserWorkStatus;
import com.wyc.serviceDriverUser.mapper.DriverUserWorkStatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wyc.serviceDriverUser.mapper.DriverUserMapper;
import com.wyc.serviceDriverUser.dto.DriverUser;
import com.wyc.serviceDriverUser.service.DriverUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 司机信息Service业务层处理
 * 
 * @author li
 * @date 2023-03-25
 */
@Service
public class DriverUserServiceImpl implements DriverUserService
{
    @Autowired
    private DriverUserMapper driverUserMapper;

    @Autowired
    private DriverUserWorkStatusMapper workStatusMapper;

    /**
     * 查询司机信息
     * 
     * @param id 司机信息主键
     * @return 司机信息
     */
    @Override
    public DriverUser selectDriverUserById(Long id)
    {
        return driverUserMapper.selectDriverUserById(id);
    }

    /**
     * 查询司机信息列表
     * 
     * @param driverUser 司机信息
     * @return 司机信息
     */
    @Override
    public List<DriverUser> selectDriverUserList(DriverUser driverUser)
    {
        return driverUserMapper.selectDriverUserList(driverUser);
    }

    /**
     * 新增或修改司机信息
     * @param driverUser
     * @return
     */
    @Transactional
    @Override
    public ApiResult insertOrUpdateDriverUser(DriverUser driverUser)
    {
        int i = 0;
        if (null == driverUser.getId()){
            driverUser.setState(DriverUserConstant.State.STATE_TRUE);
            i = driverUserMapper.insert(driverUser);

            // 新增司机工作状态
            DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
            driverUserWorkStatus.setDriverId(driverUser.getId());
            driverUserWorkStatus.setWorkStatus(DriverUserConstant.WorkStatus.WORK_STATUS_STOP);
            workStatusMapper.insert(driverUserWorkStatus);
        }else {
            i = driverUserMapper.updateById(driverUser);
        }

        return ApiResult.success(i);
    }

    /**
     * 修改司机信息
     * 
     * @param driverUser 司机信息
     * @return 结果
     */
    @Override
    public int updateDriverUser(DriverUser driverUser)
    {
        return driverUserMapper.updateDriverUser(driverUser);
    }

    /**
     * 批量删除司机信息
     * 
     * @param ids 需要删除的司机信息主键
     * @return 结果
     */
    @Override
    public int deleteDriverUserByIds(Long[] ids)
    {
        return driverUserMapper.deleteDriverUserByIds(ids);
    }

    /**
     * 删除司机信息信息
     * 
     * @param id 司机信息主键
     * @return 结果
     */
    @Override
    public int deleteDriverUserById(Long id)
    {
        return driverUserMapper.deleteDriverUserById(id);
    }

    /**
     * list
     * @return
     */
    @Override
    public ApiResult list() {
        return ApiResult.success(driverUserMapper.selectList(null));
    }

    /**
     * 查询司机信息
     * @param driverPhone
     * @return
     */
    @Override
    public ApiResult query(String driverPhone) {
        QueryWrapper<DriverUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_phone", driverPhone);
        queryWrapper.eq("state", DriverUserConstant.State.STATE_TRUE);
        Integer count = driverUserMapper.selectCount(queryWrapper);
        if (count <= 0){
            return ApiResult.error(CommonStatusEnum.DRIVER_USER_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_USER_NOT_EXISTS.getMsg());
        }
        return ApiResult.success(1);
    }

    /**
     * 通过试图查询当前城市是否存在司机。
     *
     * @param cityCode
     * @return
     */
    @Override
    public ApiResult<Boolean> queryDriverByCityCode(String cityCode) {
        Integer count = driverUserMapper.queryDriverByCityCode(cityCode);
        boolean flag = false;
        if (count > 0){
            flag = true;
        }
        return ApiResult.success(flag);
    }
}
